package com.ruoyi.web.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Product;
import com.ruoyi.system.domain.ProductCategory;
import com.ruoyi.system.domain.ProductKey;
import com.ruoyi.system.mapper.ProductCategoryMapper;
import com.ruoyi.system.mapper.ProductKeyMapper;
import com.ruoyi.web.service.IProductCategoryService;
import com.ruoyi.web.service.ISysProductKeySevice;
import com.ruoyi.web.service.ISysProductSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 商品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2022-05-03
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private ISysProductSevice productSevice;

    /**
     * 查询商品分类
     *
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public ProductCategory selectProductCategoryById(Long id)
    {
        return productCategoryMapper.selectProductCategoryById(id);
    }

    @Override
    public List<ProductCategory> selectProductCategoryById(List<Long> ids) {
        return productCategoryMapper.selectProductCategoryByIds(ids);
    }

    /**
     * 查询商品分类列表
     *
     * @param productCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<ProductCategory> selectProductCategoryList(ProductCategory productCategory) {
        return productCategoryMapper.selectProductCategoryList(productCategory);
    }

    /**
     * 新增商品分类
     *
     * @param productCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertProductCategory(ProductCategory productCategory)
    {
        productCategory.setCreateTime(DateUtils.getNowDate());
        return productCategoryMapper.insertProductCategory(productCategory);
    }

    /**
     * 修改商品分类
     *
     * @param productCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateProductCategory(ProductCategory productCategory)
    {
        return productCategoryMapper.updateProductCategory(productCategory);
    }

    /**
     * 批量删除商品分类
     *
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteProductCategoryByIds(Long[] ids) {
        List<Product> lists = productSevice.queryByCategoryId(Arrays.asList(ids));
        if (CollectionUtil.isNotEmpty(lists)) {
            throw new ServiceException("所选分类下存在商品，请先删除");
        }
        return productCategoryMapper.deleteProductCategoryByIds(ids);
    }
    @Override
    public int delete(ProductCategory productCategory) {
        // 判断该分类下是否存在商品
        return productCategoryMapper.delete(productCategory);
    }

    /**
     * 删除商品分类信息
     *
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteProductCategoryById(Long id) {
        List<Product> lists = productSevice.queryByCategoryId(Arrays.asList(id));
        if (CollectionUtil.isNotEmpty(lists)) {
            throw new ServiceException("所选分类下存在商品，请先删除");
        }
        return productCategoryMapper.deleteProductCategoryById(id);
    }
}
